// pages/driver_list/driver_list.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading:true,     // 订单状态
    status: 0,
	content:[],
    page:1,           //页码
    totalpage:1,      //总页数
    isLoading: false  // 上拉加载的时候控制scroll-view 加载提示显示或隐藏
  },
  // 切换tab
  tab: function (event) {

    var that = this;
    var status = event.currentTarget.dataset.status;

    that.setData({
      status: status,
      page:1,
      totalpage:1
    });
    that.getData();

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // that.getData();
  },

  // 获取用户输入
  input: function (res) {
	  var that = this;
	  var value = res.detail.value;
	  that.setData({
		  value: value
	  })
  },

  //搜索
  search: function (res) {
	  var that = this;
	  if (that.data.value == '' || that.data.value == undefined) {
		  wx.showModal({
			  title: '提示',
			  content: '请输入司机名或者车牌号',
		  })
	  } else {
		  app.getLoginSession(function (session) {
			  wx.request({
				  url: app.api + '/wx_log_test/driver/query',
				  data: {
					  token: session,
					  driverNameOrLicense: that.data.value,
					//   page: 1,
					//   perpage: 1000
				  },
				  success: function (e) {
					  console.log(e);
					  var res = e.data;
					  if (res.msg == "OK") {
						  var data = res.data;
						  if (data.length == 0) {
							  wx.showToast({
								  title: '暂无相关数据！',
							  })
						  } else {
							  that.setData({
								  content: data,
								  loadTips: '暂无更多数据'
							  })
						  }
					  } else {
						  wx.showModal({
							  title: '提示',
							  content: '接口请求错误：' + res.msg
						  })
					  }
				  },
				  fail: function (res) {
					  wx.showModal({
						  title: '提示',
						  content: '接口请求错误'
					  })
				  }
			  })
		  })

	  }
  },
  //获取司机列表
  getData:function(res){
    var that = this;
    if (that.data.page <= that.data.totalpage){
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api + '/wx_log_test/sys/approve',
          data: {
            token: session,
            page: that.data.page,
            perpage: 10,
            status: that.data.status
          },
          success: function (res) {
            console.log(res);
            var res = res.data;
            if (res.msg == "OK") {
              var data = res.data;
              if (data.content.length == 0) {
                that.setData({
                  loading: false,
                  loadtips: '暂无相关数据',
                  content:[]
                })
              } else {
                // 如果有数据先将数据合并 再重新设置 content
				  console.log(data.content);
				  console.log(that.data.content);
                var list = that.data.content.concat(data.content);
                that.setData({
                  loading: false,
                  totalpage: data.totalPages,
                  content: list,
                  page:that.data.page+1,
                  loadtips:'',
                  isLoading: false  // 隐藏底部加载提示
                })
              }
            } else {
              that.setData({
                loading: false,
                loadtips: res.msg
              })
            }
          },
          fail: function (res) {
            that.setData({
              loading: false,
              loadtips: '接口请求失败'
            })
          }
        })
      })
    }else{
      console.log("jdljflsdjfl");
    }
    
  },
  onShow: function () {
    this.setData({
      loading: true,
      // 订单状态
      page: 1,//页码，
      totalpage: 1,//总页数
    })
    this.getData();
  },
  onShareAppMessage: function () {
  
  },
  call: function (res) {
    wx.makePhoneCall({
      phoneNumber: res.currentTarget.dataset.num //仅为示例，并非真实的电话号码
    })
  },

  // 上拉加载函数
  lower() {
    // 显示加载提示
    this.setData({
      isLoading: true
    })
    // 发送请求加载数据
    this.getData();
  },


  // 下拉刷新数据
  upper() {
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api + '/wx_log_test/sys/approve',
        data: {
          token: session,
          page: 1,
          perpage: 10,
          status: that.data.status
        },
        success: function (res) {
          console.log(res);
          var res = res.data;
          if (res.msg == "OK") {
            var data = res.data;
            if (data.content.length == 0) {
              that.setData({
                loading: false,
                loadtips: '暂无相关数据',
                content: []
              })
            } else {
              
              that.setData({
                loading: false,
                totalpage: data.totalPages,
                content: data.content,
                page: that.data.page + 1,
                loadtips: '',
                isLoading: false  // 隐藏底部加载提示
              })
            }
          } else {
            that.setData({
              loading: false,
              loadtips: res.msg
            })
          }
        },
        fail: function (res) {
          that.setData({
            loading: false,
            loadtips: '接口请求失败'
          })
        }
      })
    })
  }
})